package http.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.SAXException;

public class HttpClient {

	public static void main(String[] args) throws UnsupportedEncodingException,
			IOException, ParserConfigurationException, SAXException {
		hello();
	}

	public static void hello() throws UnsupportedEncodingException,
			IOException, ParserConfigurationException, SAXException {
		String httpUrl = "http://192.168.0.74:8080/simple-webapp/json.do";
		HttpURLConnection http = null;
		BufferedReader bufferedReader = null;
		StringBuilder response = null;
		http = (HttpURLConnection) new URL(httpUrl).openConnection();
		http.connect();

		bufferedReader = new BufferedReader(new InputStreamReader(
				http.getInputStream()));
		String responeLine;

		response = new StringBuilder();

		while ((responeLine = bufferedReader.readLine()) != null) {
			response.append(responeLine);
		}
		System.out.println(response.toString());

		System.out.println();
		System.out.println();
		
//		JSONObject json = null;
		JSONArray jArr = null;
		try {
//			json = new JSONObject(response.toString());
//			System.out.println(json.get("hello"));
//			System.out.println(json.get("arr"));
//			jArr = (JSONArray) json.get("arr");
			jArr = new JSONArray(response.toString());
			System.out.println(jArr.get(0));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
